/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

@title: Card Match
@author: unknown
@tags: []
@addedOn: 2024-01-01
@description: A memory card matching game where you flip cards to find matching pairs. Keep track of card positions and match all pairs to win!
*/


var cards_flipped = 0
var game_started = false
var total_flipped = 0

var cards_1_14 = 0
var cards_2_8 = 0
var cards_3_9 = 0
var cards_4_12 = 0
var cards_5_6 = 0
var cards_7_13 = 0
var cards_10_16 = 0
var cards_11_15 = 0

const flip_1 = "!"
const flip_14 = "@"
const flip_2 = "#"
const flip_8 = "$"
const flip_3 = "%"
const flip_9 = "^"
const flip_4 = "&"
const flip_12 = "*"
const flip_5 = "("
const flip_6 = ")"
const flip_7 = "1"
const flip_13 = "2"
const flip_10 = "3"
const flip_16 = "4"
const flip_11 = "5"
const flip_15 = "6"

const player = "p"
const card1 = "c"
const card2 = "d"
const card3 = "g"
const card4 = "h"
const card5 = "i"
const card6 = "j"
const card7 = "k"
const card8 = "l"
const card9 = "m"
const card10 = "n"
const card11 = "o"
const card12 = "q"
const card13 = "r"
const card14 = "s"
const card15 = "t"
const card16 = "u"
const edge = "e"

var prev_card = card1
var prev_flip = flip_1
var prev_match = cards_1_14

setLegend(
  [ player, bitmap`
4444444444444444
4..............4
4..............4
4..............4
4..............4
4..............4
4..............4
4..............4
4..............4
4..............4
4..............4
4..............4
4..............4
4..............4
4..............4
4444444444444444` ],
  [ card1, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card2, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card3, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card4, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card5, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card6, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card7, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card8, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card9, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card10, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card11, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card12, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card13, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card14, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card15, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ card16, bitmap `
................
................
................
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HH6HHHH6HH...
...HH6H66H6HH...
...HH666666HH...
...HH666666HH...
...HHHHHHHHHH...
...HHHHHHHHHH...
...HHHHHHHHHH...
................
................
................`],
  [ edge, bitmap `
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111`],
  [ flip_1, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H22266222H...
...H22666622H...
...H26600662H...
...H26600662H...
...H22666622H...
...H22266222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [ flip_14, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H22266222H...
...H22666622H...
...H26600662H...
...H26600662H...
...H22666622H...
...H22266222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [ flip_2, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H27222272H...
...H22722722H...
...H22277222H...
...H22277222H...
...H22722722H...
...H27222272H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [ flip_8, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H27222272H...
...H22722722H...
...H22277222H...
...H22277222H...
...H22722722H...
...H27222272H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [ flip_3, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H22233222H...
...H24242242H...
...H24224242H...
...H22442422H...
...H22424422H...
...H22244222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [ flip_9, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H22233222H...
...H24242242H...
...H24224242H...
...H22442422H...
...H22424422H...
...H22244222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [ flip_4, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H22822822H...
...H28288282H...
...H28222282H...
...H22822822H...
...H22288222H...
...H22000022H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [ flip_12, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H22822822H...
...H28288282H...
...H28222282H...
...H22822822H...
...H22288222H...
...H22000022H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [flip_5, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H222FF222H...
...H222FF222H...
...H2FFFFFF2H...
...H2FFFFFF2H...
...H222FF222H...
...H222FF222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [flip_6, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H222FF222H...
...H222FF222H...
...H2FFFFFF2H...
...H2FFFFFF2H...
...H222FF222H...
...H222FF222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [flip_7, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H27299272H...
...H22799722H...
...H29999992H...
...H29999992H...
...H22799722H...
...H27299272H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [flip_13, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H27299272H...
...H22799722H...
...H29999992H...
...H29999992H...
...H22799722H...
...H27299272H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [flip_10, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H2LLLLLL2H...
...H2LLLLLL2H...
...H222LL222H...
...H222LL222H...
...H222LL222H...
...H222LL222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [flip_16, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H2LLLLLL2H...
...H2LLLLLL2H...
...H222LL222H...
...H222LL222H...
...H222LL222H...
...H222LL222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [flip_11, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H22222222H...
...H22266222H...
...H22622622H...
...H26222262H...
...H26666662H...
...H22222222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`],
  [flip_15, bitmap `
................
................
................
...HHHHHHHHHH...
...H22222222H...
...H22222222H...
...H22266222H...
...H22622622H...
...H26222262H...
...H26666662H...
...H22222222H...
...H22222222H...
...HHHHHHHHHH...
................
................
................`])

setSolids([player, edge])

let level = 0
const levels = [
  map`
eeeeeeee
eecdghee
eeijklee
eemnoqee
eerstuee
eeeeeeee`,
  map `
eeeeeeee
ee!#%*ee
ee((1#ee
ee%35*ee
ee1!53ee
eeeeeeee`
]


setMap(levels[level])

setPushables({
  [ player ]: []
})

onInput("w", () => {
  getFirst(player).y -= 1
})
onInput("a", () => {
  getFirst(player).x -= 1
})
onInput("s", () => {
  getFirst(player).y += 1
})
onInput("d", () => {
  getFirst(player).x += 1
})

onInput("i", () => {
  if (!game_started) {
    game_started = true
    addSprite(2, 1, player)
    clearText()
    addText("Press 'k' to flip", { 
    x: 2,
    y: 14,
    color: color`6`
    })
    }
})

onInput("k", () => {
  if ((getFirst(player).x == 2) & (getFirst(player).y == 1)){
    getFirst(card1).type = flip_1
    cards_1_14 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_1_14 < 2)) {
      cards_1_14 = 0
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_1).type = card1
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_1_14 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card1
      prev_flip = flip_1
      prev_match = cards_1_14
    }
  }
  if ((getFirst(player).x == 3) & (getFirst(player).y == 4)){
    getFirst(card14).type = flip_14
    cards_1_14 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_1_14 < 2)) {
      cards_1_14 = 0
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_14).type = card14
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_1_14 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card14
      prev_flip = flip_14
      prev_match = cards_1_14
    }
  }
  if ((getFirst(player).x == 3) & (getFirst(player).y == 1)){
    getFirst(card2).type = flip_2
    cards_2_8 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_2_8 < 2)) {
      cards_2_8 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_2).type = card2
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_2_8 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card2
      prev_flip = flip_2
      prev_match = cards_2_8
    }
  }
  if ((getFirst(player).x == 5) & (getFirst(player).y == 2)) {
    getFirst(card8).type = flip_8
    cards_2_8 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_2_8 < 2)) {
      cards_2_8 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_8).type = card8
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_2_8 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card8
      prev_flip = flip_8
      prev_match = cards_2_8
    }
  }
  if ((getFirst(player).x == 4) & (getFirst(player).y == 1)) {
    getFirst(card3).type = flip_3
    cards_3_9 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_3_9 < 2)) {
      cards_3_9 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_3).type = card3
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_3_9 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card3
      prev_flip = flip_3
      prev_match = cards_3_9
    }
  }
  if ((getFirst(player).x == 2) & (getFirst(player).y == 3)) {
    getFirst(card9).type = flip_9
    cards_3_9 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_3_9 < 2)) {
      cards_3_9 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_9).type = card9
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_3_9 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card9
      prev_flip = flip_9
      prev_match = cards_3_9
    }
  }
  if ((getFirst(player).x == 5) & (getFirst(player).y == 1)) {
    getFirst(card4).type = flip_4
    cards_4_12 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_4_12 < 2)) {
      cards_4_12 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_4).type = card4
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_4_12 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card4
      prev_flip = flip_4
      prev_match = cards_4_12
    }
  }
  if ((getFirst(player).x == 5) & (getFirst(player).y == 3)) {
    getFirst(card12).type = flip_12
    cards_4_12 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_4_12 < 2)) {
      cards_4_12 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_12).type = card12
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_4_12 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card12
      prev_flip = flip_12
      prev_match = cards_4_12
    }
  }
  if ((getFirst(player).x == 2) & (getFirst(player).y == 2)) {
    getFirst(card5).type = flip_5
    cards_5_6 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_5_6 < 2)) {
      cards_5_6 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_5).type = card5
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_5_6 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card5
      prev_flip = flip_5
      prev_match = cards_5_6
    }
  }
  if ((getFirst(player).x == 3) & (getFirst(player).y == 2)) {
    getFirst(card6).type = flip_6
    cards_5_6 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_5_6 < 2)) {
      cards_5_6 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_6).type = card6
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_5_6 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card6
      prev_flip = flip_6
      prev_match = cards_5_6
    }
  }
  if ((getFirst(player).x == 4) & (getFirst(player).y == 2)) {
    getFirst(card7).type = flip_7
    cards_7_13 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_7_13 < 2)) {
      cards_7_13 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_7).type = card7
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_7_13 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card7
      prev_flip = flip_7
      prev_match = cards_7_13
    }
  }
  if ((getFirst(player).x == 2) & (getFirst(player).y == 4)) {
    getFirst(card13).type = flip_13
    cards_7_13 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_7_13 < 2)) {
      cards_7_13 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_13).type = card13
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_7_13 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card13
      prev_flip = flip_13
      prev_match = cards_7_13
    }
  }
  if ((getFirst(player).x == 3) & (getFirst(player).y == 3)) {
    getFirst(card10).type = flip_10
    cards_10_16 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_10_16 < 2)) {
      cards_10_16 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_10).type = card10
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_10_16 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card10
      prev_flip = flip_10
      prev_match = cards_10_16
    }
  }
  if ((getFirst(player).x == 5) & (getFirst(player).y == 4)) {
    getFirst(card16).type = flip_16
    cards_10_16 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_10_16 < 2)) {
      cards_10_16 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_11_15) {cards_11_15 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_16).type = card16
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_10_16 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card16
      prev_flip = flip_16
      prev_match = cards_10_16
    }
  }
  if ((getFirst(player).x == 4) & (getFirst(player).y == 3)) {
    getFirst(card11).type = flip_11
    cards_11_15 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_11_15 < 2)) {
      cards_11_15 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_11).type = card11
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_11_15 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card11
      prev_flip = flip_11
      prev_match = cards_11_15
    }
  }
  if ((getFirst(player).x == 4) & (getFirst(player).y == 4)) {
    getFirst(card15).type = flip_15
    cards_11_15 += 1
    cards_flipped += 1
    if ((cards_flipped == 2) & (cards_11_15 < 2)) {
      cards_11_15 = 0
      if (prev_match == cards_1_14) {cards_1_14 = 0}
      if (prev_match == cards_2_8) {cards_2_8 = 0}
      if (prev_match == cards_3_9) {cards_3_9 = 0}
      if (prev_match == cards_4_12) {cards_4_12 = 0}
      if (prev_match == cards_5_6) {cards_5_6 = 0}
      if (prev_match == cards_7_13) {cards_7_13 = 0}
      if (prev_match == cards_10_16) {cards_10_16 = 0}
      setTimeout(() => {
        cards_flipped = 0
        getFirst(flip_15).type = card15
        getFirst(prev_flip).type = prev_card
      }, 1000)
    }
    if (cards_11_15 == 2) {
      total_flipped += 2
      cards_flipped = 0
    }
    if (cards_flipped == 1) {
      prev_card = card15
      prev_flip = flip_15
      prev_match = cards_11_15
    }
  }
  if (total_flipped == 16){
    clearText()
    getFirst(player).remove()
    level = level + 1
    addText("You Win!", { 
      x: 6,
      y: 1,
      color: color`6`
    })
  }
})

afterInput(() => {
  
})


addText("Press 'i' to Start", { 
  x: 1,
  y: 1,
  color: color`6`
})